﻿jQuery.fn.extend({
    editDiv: function (options) {
        if (options == undefined) options = new Object();
        createEditDiv(options, $(this));
    }
});

var editButtonId;

function createEditDiv(options, div) {
    var content = '';

    editButtonId = 'editButton' + GUID();

    var buttons = "buttons" + div[0].id;
    
    if (options.divButtonsId != undefined && options.divButtonsId != null)
        buttons = options.divButtonsId;
    
    var buttonsExisting = div.find(".buttons");

    if (buttonsExisting.length == 0) {
        if (options.editable == undefined || options.editable == true) {
            content += '<div id="' + editButtonId + '" class="floatRight button editButton">&nbsp;</div>';

            var editForm = getEditForm(options.editForm);
            editForm.dialog({ autoOpen: false, modal: true, resizable: false });
        }
        if (options.deletable == undefined || options.deletable == true) {
            content += '<div id="deleteButton' + options.editForm + '" class="floatRight button deleteButton">&nbsp;</div>';
        }

        div.before('<div id="' + buttons + '" class="floatRight transparentImage">' + content + '</div>');
    }
    else {
        buttonsExisting.id = buttons;
        buttonsExisting.addClass("floatRight transparentImage");
    }
    
    div.corner("round 10px");

    setEvents(options, div, buttons);
    setFormSubmit(options);
}

function setEvents(options, div, buttons) {
    $(div).mouseover(function () { show(div, buttons); });
    $(div).mouseout(function () { hide(div, buttons); });
    $('#' + buttons).mouseover(function () { show(div, buttons); });
    $('#' + buttons).mouseout(function () { hide(div, buttons); });

    if (options.editable == undefined || options.editable == true) {
        var editForm = getEditForm(options.editForm);
        $("#" + editButtonId).click(function () {
            editForm.dialog({ autoOpen: true, resizable: false, draggable: false });
            if (options.editClickEvent != undefined) {
                options.editClickEvent();
            }
        });
    }
    if (options.deletable == undefined || options.deletable == true) {

    }


    //    if (options.editButtonClick != undefined) {
    //        $("#editButton").click(options.editButtonClick);
    //    }

    //    if (options.deleteButtonClick != undefined) {
    //        $("#deleteButton").click(options.deleteButtonClick);
    //    }
}

function setFormSubmit(options, div) {
    if (options.formId == undefined || options.formId == null) return;

    var formId = options.formId;

    $("#" + formId).submit(function () {
        debugger;
        
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                $(div).html(result.html);
            }
        });

        var editForm = getEditForm(options.editForm);

        editForm.dialog('close');
        return false;
    });
}

function show(div, buttons) {
    div.addClass("divEditContentSelected");
    $("#" + buttons).removeClass("transparentImage");
}

function hide(div, buttons) {
    div.removeClass("divEditContentSelected");
    $("#" + buttons).addClass("transparentImage");
}

function getEditForm(editForm) {
    var editFormControl;

    if (editForm != undefined) {
        editFormControl = $("#" + editForm);
    } else {
        editFormControl = $("#editForm");
    }

    return editFormControl;
}

function GUID() {
    var s4 = function () {
        return Math.floor(
                Math.random() * 0x10000 /* 65536 */
            ).toString(16);
    };

    return (
            s4() + s4() + "-" +
            s4() + "-" +
            s4() + "-" +
            s4() + "-" +
            s4() + s4() + s4()
        );
}
